/**
 * 
 */
package ac.svn.accesscontrol.ui.renderer;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;

import ac.svn.accesscontrol.store.ISVNItem;
import ac.svn.accesscontrol.ui.IListItemTextFormatter;
import ac.svn.accesscontrol.utils.Utility;

/**
 * @author Ankush
 *
 */
public class SimpleIconRenderer extends JLabel implements ListCellRenderer, Cloneable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Border defBorder 				= null;
    private Border selBorder  				= null;
    private IListItemTextFormatter textFormatter 	= null;
    private Color defaultColor 				= Color.BLACK;
    private Color selectedColor 			= Color.BLACK;
    
    
    public SimpleIconRenderer(String resourceUri){
	super("",JLabel.LEADING);
	setIcon(Utility.getImageIcon(resourceUri));
	setOpaque(true);
	this.defBorder = getBorder();
    }


    @Override
    public Component getListCellRendererComponent(JList list, Object value,
	    int index, boolean isSelected, boolean cellHasFocus) {
	ISVNItem item = (ISVNItem) value;
	if(null != value)
	{
	    if(null != textFormatter)
	    {
		setText(textFormatter.formatString(value));
		setToolTipText(textFormatter.formatToolTip(value));
	    }
	    else
	    {
		setText(item.getName());
		setToolTipText(item.getTooltipText());
	    }

	}
	else
	{
	    setText("...");
	}
	if(isSelected)
	{
	    setBackground(new Color(104,204,255));
	    setForeground(selectedColor);
	    if(null != selBorder)
	    {
		setBorder(selBorder);
	    }
	}
	else
	{
	    setForeground(defaultColor);
	    setBackground(Color.WHITE);
	    setBorder(defBorder);
	}
	setForeground(Color.BLACK);

	return this;
    }


    public Border getDefBorder() {
	return defBorder;
    }


    public void setDefBorder(Border defBorder) {
	this.defBorder = defBorder;
    }


    public Border getSelBorder() {
	return selBorder;
    }


    public void setSelBorder(Border selBorder) {
	this.selBorder = selBorder;
    }


    public IListItemTextFormatter getTextFormatter() {
	return textFormatter;
    }


    public void setTextFormatter(IListItemTextFormatter textFormatter) {
	this.textFormatter = textFormatter;
    }


    public Color getDefColor() {
        return defaultColor;
    }


    public void setDefColor(Color defColor) {
        this.defaultColor = defColor;
    }


    public Color getSelectedColor() {
        return selectedColor;
    }


    public void setSelectedColor(Color selectedColor) {
        this.selectedColor = selectedColor;
    }

}
